package com.worklikedance.util;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.util.Base64;

public class Utilities {
	public static boolean saveArray(String[] array, String arrayName, Context mContext) {   
	    SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);  
	    SharedPreferences.Editor editor = prefs.edit();  
	    editor.putInt(arrayName +"_size", array.length);  
	    for(int i=0;i<array.length;i++)  
	        editor.putString(arrayName + "_" + i, array[i]);  
	    return editor.commit();  
	} 
	
	public static String[] loadArray(String arrayName, Context mContext) {  
	    SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);  
	    int size = prefs.getInt(arrayName + "_size", 0);  
	    String array[] = new String[size];  
	    for(int i=0;i<size;i++)  
	        array[i] = prefs.getString(arrayName + "_" + i, null);  
	    return array;  
	}  
	
	public static void storeData(String key, String stringData, Context context) {
		SharedPreferences preferences = context.getSharedPreferences("Preferences", Context.MODE_PRIVATE);  
		SharedPreferences.Editor editor = preferences.edit();
		editor.putString(key, stringData);
		editor.commit();
	}
	
	public static String getData(String key, Context context) {
		SharedPreferences preferences = context.getSharedPreferences("Preferences", Context.MODE_PRIVATE);
		
		String data = "";
		
		if (preferences != null) {
			data = preferences.getString(key, "");	
		}
		
		return data;
	}
	
	public static boolean isTablet(Context context) {				
	    boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
	    boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
	    return (xlarge || large);
	}
	
	public static int getDP(Context context, int value) {
		return (int)(value * context.getResources().getDisplayMetrics().density);
	}
	
	public static String convertStringToDateString(String date, String inputFormat, String outputFormat) {
	    Date dateObj = convertStringToDate(date, inputFormat);

	    return convertDateToString(dateObj, outputFormat);
	}
	
	@SuppressLint("SimpleDateFormat")
	public static Date convertStringToDate(String dateString, String format) {
	    SimpleDateFormat dateFormat = new SimpleDateFormat(format);
	    Date convertedDate = null;
	    
	        try {
				convertedDate = dateFormat.parse(dateString);
			} catch (java.text.ParseException e) {
				e.printStackTrace();
			}
	    
	    return convertedDate;
	}
	
	@SuppressLint("SimpleDateFormat")
	public static String convertDateToString(Date date, String format) {
		if (date == null) {
			return "";
		}
		
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		
		return dateFormat.format(date);		
	}
	
	public static String formatNumber(String number) {
		
		//thay dau . = , cho nguoi viet
	    String simpleNumber = number.replace(".", ",");
	    if (simpleNumber.length() == 0) {
	        return "";
	    }
	    
	    String result = "";
	    int count = 0;
	    String dot = ".";	    
	    
	    for (int i = simpleNumber.length() - 1; i >= 0; i--) {
	        
	        char ch = simpleNumber.charAt(i);	        
	        result = ch + result;
	        
	        if (ch != ',') {
	        	count++;
			}
	        else {
	        	result = result.replace(".", "");
				count = 0;
			}
	        	        
	        if (count > 0 && count % 3 == 0 && i > 0) {
	        	result = dot + result;	            
	        }
	    }
	    
	    //fix case -.230 = -230
	    result = result.replace("-.", "-");
	    
	    return result;
	}
	
	public static long compareDateWithoutSecond(Date a, Date b) {
		long diffInMs = a.getTime() - b.getTime();
		long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMs);
		
		return diffInSec;
	}
	
	public static String getUri(String eUri) {
		byte []data = Base64.decode(eUri, Base64.DEFAULT);
		
		String uri = "";
		
		try {
			uri = new String(data, "utf-8");
		} 
		catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		return uri;
	}
}
